﻿using System;
using System.Windows.Forms;
using System.Net.Mail;
using System.IO;
using System.Collections.Generic;
using System.Collections;

namespace KiraSh.ResumeTracker
{
    public partial class frmEmailForm : Form
    {
        private static frmEmailForm instance = null;
        private XMLDealer xml = new XMLDealer();

        private frmEmailForm()
        {
            InitializeComponent();
            this.Text = CurrentContext.Name + " | Profile: " + CurrentContext.Profile.Name;
        }

        public static frmEmailForm FormShow()
        {
            if (instance == null)
            {
                instance = new frmEmailForm();
            }

            instance.Show();
            instance.BringToFront();

            return instance;

        }

        private void comboBoxLoad(string xmlPath, ComboBox bx, Type t, object select) 
        {
            object o = xml.LoadXML(xmlPath, t);
            if (o != null) 
            {
                foreach (object obj in (IEnumerable) o)
                {
                    int i = bx.Items.Add(obj);
                    if (obj.Equals(select))
                    {
                        bx.SelectedIndex = i;
                    }
                }
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                string server = "smtp.gmail.com";
                int port = 587;
                string username = "";
                string password = "";
                SmtpClient mailBox = new SmtpClient(server, port);
                mailBox.EnableSsl = true;
                mailBox.UseDefaultCredentials = false;
                mailBox.Credentials = new System.Net.NetworkCredential(username, password);
                mailBox.DeliveryMethod = SmtpDeliveryMethod.Network;
                mailBox.Send(createMes());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private MailMessage createMes()
        {
            MailMessage mail = new MailMessage("", "", "test .Net email", "test .Net email");
            return mail;
        }

        private void frmEmailForm_Load(object sender, EventArgs e)
        {
            comboBoxLoad(PathHelper.MapCombine(CurrentContext.Profile + ".xml"), bxCompany, typeof(List<Company>), CurrentContext.Company);
            comboBoxLoad(PathHelper.MapCombine(CurrentContext.Profile.Name, "EmailServers.xml"), bxSelectEmail, typeof(List<EmailServer>), null);
            for (int i=0; i<bxSelectEmail.Items.Count;i++)
            {
                EmailServer srv = (EmailServer)bxSelectEmail.Items[i];
                if (!srv.IsFullySetup)
                {
                    bxSelectEmail.Items.RemoveAt(i);
                    i--;
                }

            }
        }

        private void frmEmailForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }
    }
}
